﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Engine.NetClasses
{
     

    
    /// <summary>
    /// Пакет отправляет Запрос на логин
    /// Type 3
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct RequestLoginAuth : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;
        /// <summary>
        /// Login 16 max symbols
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        private char[] _login;
        /// <summary>
        /// password max 20 symbols
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray,SizeConst = 20)]
        private char[] _password;

        #endregion

        #region Constructor
       
        public RequestLoginAuth(string Login, string pass)
            : this(true, false, 3)
        {
            if ((Login.Length>0 && Login.Length<=16) && (pass.Length>0 && pass.Length<=20))
            {
                
           
            _login = new char[16];
            Login.CopyTo(0, _login, 0, Login.Length);           
            _password = new char[20];
            pass.CopyTo(0, _password, 0, pass.Length);
            }
        }
        public RequestLoginAuth(byte[] data)
            : this(true, false, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _login = new char[16];
            Encoding.ASCII.GetChars(data, 3, 16 * 2, _login, 0);
            _password = new char[20];
            Encoding.ASCII.GetChars(data, 16 * 2 + 3, 20 * 2, _password, 0);
        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public RequestLoginAuth(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 1+16*2+20*2;
            _login = new char[16];            
            _password = new char[20];
           

        }
        public void SetLoginAndPass(string Login, string pass)
        {
            if ((Login.Length > 0 && Login.Length <= 16) && (pass.Length > 0 && pass.Length <= 20))
            {


                _login = new char[16];
                Login.CopyTo(0, _login, 0, Login.Length);
                _password = new char[20];
                pass.CopyTo(0, _password, 0, pass.Length);
            }
        }
        #endregion
        #region Propertes
        public bool DoubleCrypt { get { return false; } }
        public char[] Login
        {
            get { return _login; }
        }
        public char[] Password
        {
            get { return _password; }
        }
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return true; }
        }
        #endregion

        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size+2];
            Encoding.ASCII.GetBytes(_login, 0, _login.Length, tmp, 3);
            Encoding.ASCII.GetBytes(_password, 0, _password.Length, tmp, 16 * 2 + 3);
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0];
            tmp[1] = s[1];
            tmp[2] = _packetType;
            return tmp;
        }        
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _login = new char[16];
            _login = Encoding.ASCII.GetChars(data, 3, 16 * 2);
            _password = new char[20];
            _password = Encoding.ASCII.GetChars(data, 16 * 2+3, 20 * 2);
        }
        public bool isNull()
        {
            if (_size == 0 && (_login == null || _password == null))
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'RequestLoginAuth', Login: '" + _login + "', Password: '" + _password + "'";
        }

    }

    /// <summary>
    /// Пакет отправляет сервер клиенту при подключении
    /// Type = 1
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct Init : Engine.NetClasses.IPacket
    {
        short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        byte _packetType;
        /// <summary>
        /// ID сессий
        /// </summary>
        public uint _id;
        /// <summary>
        /// номер протокола
        /// </summary>
        public int _protocol;

        #region Constructor
        
        public Init(byte[] data)
            : this(false, true, 1)
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _id = BitConverter.ToUInt32(data, 3);
            _protocol = BitConverter.ToInt32(data, 7);
        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public Init(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 1+4+4;
            _id = 0;
            _protocol = 0;
        }
        #endregion
        #region Propertes
        public bool DoubleCrypt { get { return false; } }
        public bool NeedCrypt
        {
            get { return false; }
        }
        public short Size
        {
            get { return _size; }
        }
        public uint ID
        {
            get { return _id; }
        }
        public int Protocol
        {
            get { return _protocol; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        
        #endregion

        public void Convert(byte[] data)
        {
            _id = BitConverter.ToUInt32(data, 3);
            _protocol = BitConverter.ToInt32(data, 7);
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
        }
        public bool isNull()
        {
            if (_size==0 || _id == 0 || _protocol == 0)
            {
                return true;
            }
            return false;
        }       
        public override string ToString()
        {
            return "Pack name: 'Init', ID: '" + _id + "', protocol: '" + _protocol + "'";
        }



        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size+2];
            Array.Copy(BitConverter.GetBytes(_id), 0, tmp, 3, 4);
            Array.Copy(BitConverter.GetBytes(_protocol), 0, tmp, 7, 4);
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0];
            tmp[1] = s[1];
            tmp[2] = _packetType;
            return tmp;
        }
    }

    /// <summary>
    /// Пакет отправляемый сервером 
    /// Логин удачный
    /// Type 2
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct LoginOk : Engine.NetClasses.IPacket
    {
        short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        byte _packetType;
       /// <summary>
       /// Первая часть ключа
       /// </summary>
        uint _frist;
        /// <summary>
        /// Вторая часть ключа
        /// </summary>
        uint _secondory;

        #region Constructor
       
        public LoginOk(byte[] data)
            : this(false, true, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _frist = BitConverter.ToUInt32(data, 3);
            _secondory = BitConverter.ToUInt32(data, 7);
           
        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public LoginOk(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 1+4+4;
            _frist=0;
            _secondory=0;
        }
        #endregion
        #region Propertes
        public bool NeedCrypt
        {
            get { return true; }
        }
        public uint Frist
        {
            get{ return _frist;}
            set { _frist = value; }
        }
        public uint Secondory
        {
            get{return _secondory;}
            set{ _secondory=value;}
        }

        public short Size
        {
            get { return _size; }
        }       
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool DoubleCrypt { get { return false; } }
        #endregion

        public void Convert(byte[] data)
        {
           
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _frist = BitConverter.ToUInt32(data, 3);
            _secondory = BitConverter.ToUInt32(data, 7);
        }
        public bool isNull()
        {
            if (_size==0)
            {
                return true;
            }
            return false;
        }       
        public override string ToString()
        {
            return "Pack name: 'LoginOk', frist: '" + _frist + "', secondory: '" + _secondory + "'";
        }
        
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            Array.Copy(BitConverter.GetBytes(_frist), 0, tmp, 3, 4);
            Array.Copy(BitConverter.GetBytes(_secondory), 0, tmp, 7, 4);
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0];
            tmp[1] = s[1];
            tmp[2] = _packetType;
            return tmp;
        }
    }
    
    /// <summary>
    /// Пакет отправляет 
    /// Type 4
    /// SizePacet 2
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct LoginFail : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;
        private byte _failCode;
        #endregion

        #region Constructor

        public LoginFail(byte[] data)
            : this(false, true, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _failCode = data[3];
        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public LoginFail(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 2;
            _failCode = 0;

        }
        #endregion
        #region Propertes
        public byte FailCode
        {
            get { return _failCode; }
            set { _failCode = value; }
        }
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return true; }
        }
        public bool DoubleCrypt { get { return false; } }
        #endregion
        #region Method
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _failCode = data[3];
        }
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0]; //размер 1 байт
            tmp[1] = s[1]; // размер 2 байт
            tmp[2] = _packetType; //тип пакета
            tmp[3] = _failCode;
            return tmp;
        }
        public bool isNull()
        {
            if (_size == 0)
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'LoginFail', code: '"+_failCode+"'";
        }
        #endregion

    }
    
    /// <summary>
    /// Пакет отправляет 
    /// Type 5
    /// SizePacet 9
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct NetPing : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;
        private uint _id;
        private int _ping;
        #endregion

        #region Constructor

        public NetPing(byte[] data)
            : this(true, true, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _id = BitConverter.ToUInt32(data, 3);
            _ping = BitConverter.ToInt32(data, 7);

        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public NetPing(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 9;
            _id = 0;
            _ping = 0;

        }
        #endregion
        #region Propertes
        public uint ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        public int Ping
        {
            get
            {
                return _ping;
            }
            set
            {
                _ping = value;
            }
        }
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return false; }
        }
        public bool DoubleCrypt { get { return false; } }
        #endregion
        #region Method
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            _id = BitConverter.ToUInt32(data, 3);
            _ping = BitConverter.ToInt32(data, 7);
        }
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            byte[] s = BitConverter.GetBytes(_size);
            Array.Copy(BitConverter.GetBytes(_id), 0, tmp, 3, 4);
            Array.Copy(BitConverter.GetBytes(_ping), 0, tmp, 7, 4);
            tmp[0] = s[0]; //размер 1 байт
            tmp[1] = s[1]; // размер 2 байт
            tmp[2] = _packetType; //тип пакета
            return tmp;
        }
        public bool isNull()
        {
            if (_size == 0 ||(_id==0 && _ping==0))
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'NetPing', ID: '" + _id + "', ping: '" + _ping + "'";
        }
        #endregion

    }
    
    
    /// <summary>
    /// Пакет отправляет 
    /// Type 6
    /// SizePacet 1
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct RequestServerList : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;
        #endregion

        #region Constructor

        public RequestServerList(byte[] data)
            : this(true, false, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];

        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public RequestServerList(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 1;

        }
        #endregion
        #region Propertes
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return true; }
        }
        public bool DoubleCrypt { get { return true; } }
        #endregion
        #region Method
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
        }
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0]; //размер 1 байт
            tmp[1] = s[1]; // размер 2 байт
            tmp[2] = _packetType; //тип пакета
            return tmp;
        }
        public bool isNull()
        {
            if (_size == 0)
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'RequestServerList'";
        }
        #endregion

    }

    
    /// <summary>
    /// Пакет отправляет 
    /// Type 7
    /// SizePacet 56
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct ListServer : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;

        private byte[] _idServer;
        private char[][] _ip;
        private int[] _port;
        private char[][] _nameServer;
        private bool[] _online;
        #endregion

        #region Constructor

        public ListServer(byte[] data)
            : this(false, true, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            ///
            // 


            if (_size-1>56)
            {
                int size=(_size-1)/56;
                _idServer = new byte[size];
                _port = new int[size];
                _ip = new char[size][];                
                _nameServer = new char[size][] ;
                _online = new bool[size];
                for (int i = 3; i < size; i++)
                {
                _idServer[i]=data[i*56+3];
                _port[i]=BitConverter.ToInt32(data,4+i*56);
                _ip[i] = new char[30];
                Encoding.ASCII.GetChars(data, i*56+8, 15 * 2, _ip[i], 0);
                _nameServer[i] =new char[20];
                Encoding.ASCII.GetChars(data, 15 * 2 + 8+i*56, 10 * 2, _nameServer[i], 0);
                _online[i]=BitConverter.ToBoolean(data,i*56+58);
                }
            }
            else
            {
                _idServer=new byte[]{data[3]};
                _port=new int[]{BitConverter.ToInt32(data,4)};
                _ip = new char[][]{new char[30]};
                Encoding.ASCII.GetChars(data, 8, 15 * 2, _ip[0], 0);
                _nameServer = new char[][]{new char[20]};
                Encoding.ASCII.GetChars(data, 15 * 2 + 8, 10 * 2, _nameServer[0], 0);
                 _online=new bool[]{ BitConverter.ToBoolean(data,58)};
            }

        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public ListServer(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _idServer = new byte[0];
            _port = new int[0];
            _nameServer = new char[0][];
            _ip = new char[0][];
            _size = 57;
            _online = new bool[0];

        }
        #endregion
        #region Propertes
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return true; }
        }
        public bool DoubleCrypt { get { return true; } }
        #endregion
        #region Method
        public void FromArray(ServersList[] data)
        {
            _size = (short)(1+(data.Length*56));
            _packetType = 7;
            if (_size - 1 > 56)
            {
                int size = (_size - 1) / 56;
                _idServer = new byte[size];
                _port = new int[size];
                _ip = new char[size][];
                _nameServer = new char[size][];
                _online = new bool[size];
                for (int i = 0; i < size; i++)
                {
                    _idServer[i] = (byte)i;
                    _port[i] = data[i].PortServer;
                    _ip[i] = new char[30];
                    data[i].IpServer.CopyTo(0, _ip[i], 0,data[i].IpServer.Length);
                 
                    _nameServer[i] = new char[20];
                    data[i].ServerName.CopyTo(0, _nameServer[i], 0, data[i].ServerName.Length);
                    _online[i] = data[i].Online;
                }
            }
            else
            {
                _idServer = new byte[] { 0 };
                _port = new int[] {data[0].PortServer };
                _ip = new char[][] { new char[30] };
                data[0].IpServer.CopyTo(0, _ip[0], 0, data[0].IpServer.Length);
                _online = new bool[] { data[0].Online };
                _nameServer = new char[][] { new char[20] };
                data[0].ServerName.CopyTo(0, _nameServer[0], 0, data[0].ServerName.Length);
               
            }
        }
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
            if (_size - 1 > 56)
            {
                int size = (_size - 1) / 56;
                _idServer = new byte[size];
                _port = new int[size];
                _ip = new char[size][];
                _nameServer = new char[size][];
                _online = new bool[size];
                for (int i = 0; i < size; i++)
                {
                    _idServer[i] = data[i * 56 + 3];
                    _port[i] = BitConverter.ToInt32(data, 4 + i * 56);
                    _ip[i] = new char[30];
                    Encoding.ASCII.GetChars(data, i * 56 + 8, 15 * 2, _ip[i], 0);
                    _nameServer[i] = new char[20];
                    Encoding.ASCII.GetChars(data, 15 * 2 + 8 + i * 56, 10 * 2, _nameServer[i], 0);
                    _online[i] = BitConverter.ToBoolean(data, i * 56 + 58);
                }
            }
            else
            {
                _idServer = new byte[] { data[3] };
                _port = new int[] { BitConverter.ToInt32(data, 4) };
                _ip = new char[][] { new char[30] };
                _online = new bool[] { BitConverter.ToBoolean(data,58) };
                Encoding.ASCII.GetChars(data, 8, 15 * 2, _ip[0], 0);
                _nameServer = new char[][] { new char[20] };
                Encoding.ASCII.GetChars(data, 15 * 2 + 8, 10 * 2, _nameServer[0], 0);
            }
        }
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0]; //размер 1 байт
            tmp[1] = s[1]; // размер 2 байт
            tmp[2] = _packetType; //тип пакета
            for (int i = 0; i < _idServer.Length; i++)
            {
            Encoding.ASCII.GetBytes(_ip[i], 0, _ip[i].Length, tmp,i*56+8);
            Encoding.ASCII.GetBytes(_nameServer[i], 0, _nameServer[i].Length, tmp,i*56+ 15 * 2 + 8);
            tmp[i * 56+3] = _idServer[i];
            Array.Copy(BitConverter.GetBytes(_online[i]), 0, tmp, i * 56 + 58,1);
            Array.Copy(BitConverter.GetBytes(_port[i]), 0, tmp,i*56+ 4, 4);
            }
            return tmp;
        }
        public bool isNull()
        {
            if (_size == 0)
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'ServerList'";
        }
        #endregion


        public ServersList[] GetArray()
        {
            ServersList[] ll = new ServersList[_idServer.Length];
            for (int i = 0; i < _idServer.Length; i++)
            {
                ll[i] =new ServersList(new string(_nameServer[i]).Replace("\0", ""),new string(_ip[i]).Replace("\0", ""),_port[i],_online[i]);
            }
            if (_idServer.Length>0)
            {
                 return ll;
            }
            else
            {
                return null;
            }
           
        }
    }

    
    /// <summary>
    /// Пакет отправляет 
    /// Type 8
    /// SizePacet 1
    /// </summary>
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Auto)]
    public struct RequestGame : Engine.NetClasses.IPacket
    {
        #region Fields
        /// <summary>
        /// Размер пакета с 3 байта
        /// </summary>
        private short _size;
        /// <summary>
        /// Этот пакет может быть от клиента
        /// </summary>
        private bool _clientPack;
        /// <summary>
        /// Этот пакет может быть от сервера
        /// </summary>
        private bool _serverPack;
        /// <summary>
        /// Тип пакета
        /// </summary>
        private byte _packetType;
        #endregion

        #region Constructor

        public RequestGame(byte[] data)
            : this(true, false, data[2])
        {
            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];

        }
        /// <summary>
        /// Новый пакет
        /// </summary>
        /// <param name="Client"> может быть от клиента</param>
        /// <param name="Server"> может быть от сервера</param>
        /// <param name="typePack"> тип пакета</param>
        public RequestGame(bool Client, bool Server, byte typePack)
        {
            _clientPack = Client;
            _serverPack = Server;
            _packetType = typePack;
            _size = 1;

        }
        #endregion
        #region Propertes
        public short Size
        {
            get { return _size; }
        }
        public bool ServerPacket
        {
            get { return _serverPack; }
        }
        public bool ClientPacket
        {
            get { return _clientPack; }
        }
        public string Name
        {
            get { return this.GetType().Name; }
        }
        public byte PacketType
        {
            get { return _packetType; }
        }
        public bool NeedCrypt
        {
            get { return true; }
        }
        public bool DoubleCrypt { get { return false; } }
        #endregion
        #region Method
        public void Convert(byte[] data)
        {

            _size = BitConverter.ToInt16(data, 0);
            _packetType = data[2];
        }
        public byte[] GetByte()
        {
            byte[] tmp = new byte[_size + 2];
            byte[] s = BitConverter.GetBytes(_size);
            tmp[0] = s[0]; //размер 1 байт
            tmp[1] = s[1]; // размер 2 байт
            tmp[2] = _packetType; //тип пакета
            return tmp;
        }
        public bool isNull()
        {
            if (_size == 0)
            {
                return true;
            }
            return false;
        }
        public override string ToString()
        {
            return "Pack name: 'RequestGame'";
        }
        #endregion

    }
 
    
}
